<script setup>
    import { ref } from 'vue'
    import { User, Lock, Key } from '@element-plus/icons-vue'
    import { ElMessage } from 'element-plus'
    import userApi from '@/api/user.js'
    import { useRouter } from 'vue-router'

    const router = useRouter()

    // 注册表单数据
    const registerForm = ref({
        name: '',
        password: '',
        confirmPassword: ''
    })

    // 表单校验规则
    const rules = ref({
        name: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 2, max: 16, message: '用户名的长度必须为2~16位', trigger: 'blur' }
        ],
        password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 16, message: '密码长度必须为3~16位', trigger: 'blur' }
        ],
        confirmPassword: [
            { required: true, message:'请确认密码', trigger: 'blur' },
            {
                validator: (rule, value, callback) => {
                    if (value !== registerForm.value.password) {
                        callback(new Error('两次输入的密码不一致'))
                    } else {
                        callback()
                    }
                },
                trigger: 'blur'
            }
        ]
    })

    // 提交注册
    const submitRegister = () => {
        userApi.register(registerForm.value).then(result => {
            if (result.code === 0) {
                ElMessage.success('注册成功，请登录')
                router.push('/login') // 注册成功后跳转到登录页
            } else {
                ElMessage.error(result.msg || '注册失败')
            }
        })
    }

    // 返回登录页
    const goToLogin = () => {
        router.push('/login')
    }
</script>

<template>
    <div class="register-bg">
        <el-form class="form-register" ref="form" size="large" autocomplete="off" :model="registerForm" :rules="rules">
            <el-form-item>
                <h1>用户注册</h1>
            </el-form-item>
            <el-form-item prop="name">
                <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerForm.name" />
            </el-form-item>
            <el-form-item prop="password">
                <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerForm.password" />
            </el-form-item>
            <el-form-item prop="confirmPassword">
                <el-input :prefix-icon="Key" type="password" placeholder="请确认密码" v-model="registerForm.confirmPassword" />
            </el-form-item>
            <el-form-item>
                <el-button class="button" type="primary" auto-insert-space @click="submitRegister">注册</el-button>
                <el-button class="button" auto-insert-space @click="goToLogin">返回登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<style scoped>
    .register-bg {
        height: 100%;
        background-repeat: no-repeat;
        background-position: center;
        background-attachment: fixed;
        background-size: cover;
    }

    .form-register {
        width: 280px;
        padding: 20px;
        position: absolute;
        top: 20%;
        left: calc(50% - 150px);
        background-color: #FFF;
        box-shadow: 10px 10px 30px #000;
    }
</style>